package info.pinlab.pinplayer.view.swing;


import info.pinlab.pinplayer.control.TaskController;
import info.pinlab.pinplayer.model.display.Display;
import info.pinlab.pinplayer.model.display.TextDisplay;
import info.pinlab.pinplayer.model.task.Task;

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

/**
 * 
 *  __________________
 * |                  |
 * |   Display        |
 * |                  |
 * |~~~~~~~~~~~~~~~~~~|
 * |                  |
 * |   Response       |
 * |__________________|
 * 
 * 
 * 
 * @author Gabor Pinter
 *
 */
@SuppressWarnings("serial")
public abstract class AbstractTaskPanel extends JPanel implements TaskViewPanel {

	private GridBagConstraints gbcTop;
	private GridBagConstraints gbcBottom ;
	TaskController taskController = null;
	private boolean isUnseen = true;
	long displayT = -1;
	
	public AbstractTaskPanel(){
		setLayout(new GridBagLayout());
		gbcTop = GbcFactory.getFillBoth();
		gbcTop.gridy = 0;
		gbcBottom = GbcFactory.getRow();
		gbcBottom.ipady = 16;
		gbcBottom.gridy = 1;
//		addComponentListener(this);
	}
	
	public void setTopPanel(Component panel){
		this.add(panel, gbcTop);
	}
		

	public void setBottomPanel(Component panel, GridBagConstraints gbc){
		this.add(panel, gbc);
	}
	
	public void setBottomPanel(Component panel){
		setBottomPanel(panel, gbcBottom);
	}
	
	@Override
	public JPanel getPanel() {
		return this;
	}
	
	@Override
	public void setTaskController(TaskController controller){
		taskController = controller;
	}
	
	
	
	@Override
	public void setTask(Task task) {
		JPanel panel = new JPanel();
		panel.setLayout(new GridBagLayout());
		int gridy = 0;
		for(Display disp : task.getDisplays()){
			if(disp instanceof TextDisplay){
				JLabel label = new JLabel(((TextDisplay)disp).getText());
				label.setOpaque(true);
				label.setBackground(Color.yellow);
				label.setHorizontalAlignment(SwingConstants.CENTER);
				GridBagConstraints gbc = GbcFactory.getFillBoth();
				gbc.gridy = gridy++;
				panel.add(label, gbc);
			}
		}
		setTopPanel(panel);
	}

	
//	@Override
//	public void componentHidden(ComponentEvent e) {
//		displayT =-1; //-- so that response times can be calculated!
//		System.out.println("Component HIDDEN!");
//	}
	
	public void paintComponent(Graphics g) {
        super.paintComponent(g);
        if(displayT<0){
        	setEnabled(true);
        	displayT = System.currentTimeMillis();
        }
        
//        System.out.println("Paint component" + isShowing());
        if(isUnseen && taskController!=null){
        	taskController.onViewVisible();
        	isUnseen=false;
        }

        //-- setting correct focus! --//
        if(defaultFocusComp!=null){
        	defaultFocusComp.requestFocus();
        }
	}
	
	
//	@Override
//	public void componentResized(ComponentEvent e) {
//		// TODO Auto-generated method stub
//		
//	}
//
//	@Override
//	public void componentMoved(ComponentEvent e) {
//		// TODO Auto-generated method stub
//		
//	}
//
//	@Override
//	public void componentShown(ComponentEvent e) {
//	}
//	
	
	JComponent defaultFocusComp = null;
	@Override
	public void setDefaultFocus(JComponent comp) {
		defaultFocusComp = comp;
	}
	
}





